
package com.eyedog.video.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eyedog.library.base.BaseRecyclerAdapter;
import com.eyedog.video.R;
import com.eyedog.video.entity.CategoryEntity;
import com.eyedog.video.videolist.VideoListActivity;

public class CategoryAdapter
        extends BaseRecyclerAdapter<CategoryEntity, CategoryAdapter.CategoryHolder> {

    public CategoryAdapter(Context context) {
        super(context);
    }

    @Override
    public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(getContext()).inflate(R.layout.item_search_category,
                parent, false);
        return new CategoryHolder(layout);
    }

    @Override
    public void onBindViewHolder(CategoryHolder holder, int position) {
        CategoryEntity entity = getItem(position);
        if (entity != null && !TextUtils.isEmpty(entity.name)) {
            holder.categoryText.setText(entity.name);
            holder.categoryText.setTag(entity);
        }
    }

    class CategoryHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView categoryText;

        public CategoryHolder(View itemView) {
            super(itemView);
            categoryText = (TextView) itemView.findViewById(R.id.tv_search_category);
            categoryText.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_search_category:
                    Object tag = view.getTag();
                    if (tag != null && tag instanceof CategoryEntity) {
                        Bundle data = VideoListActivity.createVideoListBundle(
                                ((CategoryEntity) tag).id, ((CategoryEntity) tag).name);
                        Intent intent = new Intent(getContext(), VideoListActivity.class);
                        intent.putExtras(data);
                        getContext().startActivity(intent);
                    }
                    break;
            }
        }
    }
}
